package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.NotesPayabledHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.purchase.NotesPayabledHeaderRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.purchase.NotesPayabledHeaderManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.collections4.MapUtils;
import cn.com.huzo.repository.purchase.NotesPayabledLinesPoStockInRepository;
import cn.com.huzo.repository.purchase.NotesPayabledLinesRepository;
import cn.com.huzo.service.purchase.NotesPayabledLinesManager;

/**
 * 对账单信息管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("NotesPayabledHeaderManager")
public class NotesPayabledHeaderManagerImpl extends GenericManagerImpl<NotesPayabledHeaderEntity, Long>
        implements NotesPayabledHeaderManager {

    @Resource(name = "NotesPayabledHeaderRepository")
    private NotesPayabledHeaderRepository _dao;

    @Resource(name = "NotesPayabledLinesPoStockInRepository")
    private NotesPayabledLinesPoStockInRepository _dao_lines_po_stockin;

    @Resource(name = "NotesPayabledLinesRepository")
    private NotesPayabledLinesRepository _dao_lines;

    @Resource(name = "NotesPayabledLinesManager")
    private NotesPayabledLinesManager _manager_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        // 得到对账单表头信息
        _result.put("HeaderEntity", _dao.fetchByKey(MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId")));
        // 到货列表
        _result.put("LinesPoStockInList", _dao_lines_po_stockin.fetchByHeaderKey(criterion));
        // 对账明细表
        _result.put("LinesData", _manager_lines.fetchForPage(criterion));
        // 本次对账总金额
        _result.put("TotalAmount", _dao_lines.fetchTotalAmount(criterion));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<NotesPayabledHeaderEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<NotesPayabledHeaderEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Long insert(NotesPayabledHeaderEntity instance) throws Exception {
        Long key = _dao.insert(instance);
        if (key != null && key > 0) {
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "采购管理→对账单主表", "单据号：" + instance.getBillNo(), "增加");
        }

        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(NotesPayabledHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao.update(instance);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "采购管理→对账单主表", "单据号：" + instance.getBillNo(), "更新");
        }
        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "HeaderId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "采购管理→对账单主表", "单据号：" + MapUtils.getString(criterion, "DeletedBillNo"),
                    "删除");
        }
        return fetchForPage(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public String fetchNewBillNo() throws Exception {
        return _dao.fetchNewBillNo();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchExportDataByHeaderId(Long headerId, Integer styleNoFlag) throws Exception {
        Map<String, Object> _result = new HashMap();
        // 得到对账单表头信息
        _result.put("HeaderEntity", _dao.fetchByKey(headerId, null, null));
        // 对账明细表              
        _result.put("LinesData", _dao_lines.fetchExportLinesData(headerId, styleNoFlag));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> fetchExportDataWithStyleNoByHeaderId(Long headerId, Integer styleNoFlag) throws Exception {
        Map<String, Object> _result = new HashMap();
        // 得到对账单表头信息
        _result.put("HeaderEntity", _dao.fetchByKey(headerId, null, null));
        // 对账明细表              
        _result.put("LinesData", _dao_lines.fetchExportLinesDataWithStyleNo(headerId, styleNoFlag));

        return _result;
    }
}
